package com.xiaomi.controller;

import com.alibaba.fastjson.JSON;
import com.xiaomi.entity.Lunbotu;
import com.xiaomi.model.Result;
import com.xiaomi.service.LunbotuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName LunbotuController
 * @Description TODO
 * @Author zcy
 * @Date 2021/11/28 23:05
 * @Version 1.0
 **/
@Controller
@RequestMapping(value = "/lbt",produces = "application/json;charset=utf-8")
public class LunbotuController {
    @Autowired
    private LunbotuService lunbotuService;
    @GetMapping("/all.do")
    @ResponseBody
    public String pageSearch(@RequestParam Integer page, @RequestParam Integer limit,
                              @RequestParam(required = false) Integer lbtId){
        Integer startRow = (page-1)*limit;
        List<Lunbotu> list =lunbotuService.pageSearch(startRow, limit, lbtId);
        Integer count = lunbotuService.getCount(lbtId);
        Result result = new Result();
        result.setData(list);
        result.setCode(0);
        result.setMsg("查询成功");
        result.setCount(count);
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/add.do")
    @ResponseBody
    public String jia(Lunbotu lunbotu){
        lunbotuService.insert(lunbotu);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("添加111111成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/update.do")
    @ResponseBody
    public String xiugai(Lunbotu lunbotu){
        lunbotuService.update(lunbotu);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("修改成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping("/del.do")
    @ResponseBody
    public String delete(@RequestParam Integer lbtId){
        lunbotuService.delete(lbtId);
        Result result = new Result();
        result.setCode(0);
        result.setMsg("删除成功");
        String json = JSON.toJSONString(result);
        return json;
    }
    @GetMapping ("/sj.do")
    @ResponseBody
    public String sjupdate(Lunbotu lunbotu){

       Integer row= lunbotuService.sjupdate(lunbotu);

        Result result = new Result();
        if (row == 1){
            result.setCode(0);
            result.setMsg("修改111111成功");
        }else {
            result.setCode(1);
            result.setMsg("修改失败");
        }

        String json = JSON.toJSONString(result);
        return json;
    }
}
